<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="应用名称" prop="appName">-->
<!--        <el-input v-model="queryParams.cateName" placeholder="请输入应用名称" clearable size="small" style="width: 240px"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <search-button @click="handleQuery" :perm="['conn:capability:categories:search']" />-->
<!--        <reset-button @click="resetQuery" :perm="['conn:capability:categories:update']" />-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <add-button @click="handleAdd" :perm="['conn:capability:categories:insert']" :roleType="['conf']" />
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :perm="['conn:capability:categories:search']" />
    </el-row>

    <el-table v-loading="loading" :data="classifyList" row-key="cateId"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column prop="cateName" label="分类名称" :show-overflow-tooltip="true" min-width="180"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true" min-width="180"></el-table-column>
      <el-table-column prop="createBy" label="创建人" :show-overflow-tooltip="true" min-width="180"></el-table-column>
      <el-table-column prop="ctxUrl" label="服务地址" :show-overflow-tooltip="true" min-width="180"></el-table-column>
      <el-table-column prop="appId" label="appId" :show-overflow-tooltip="true" min-width="180"></el-table-column>
      <el-table-column prop="appSecret" label="appSecret" :show-overflow-tooltip="true" min-width="180"></el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160"
        v-if="hasRoleType(['conf'])">
        <template #default="scope">
          <text-add-button @click="handleAdd(scope.row)" :perm="['conn:capability:categories:insert']" :roleType="['conf']" />
          <text-edit-button @click="handleUpdate(scope.row)" :perm="['conn:capability:categories:update']" :roleType="['conf']" />
          <text-delete-button @click="handleDelete(scope.row)" :perm="['conn:capability:categories:delete']" :roleType="['conf']"
            :title="'是否确认删除' + scope.row.cateName + '?'" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total"  v-model:page="queryParams.pageNum"  v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog v-el-drag-dialog :title="title" :close-on-click-modal="false" v-model="open" width="500px"
      append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父节点">
          <el-input v-model="form.cateParentName" disabled />
        </el-form-item>
        <el-form-item label="分类名称" prop="cateName">
          <el-input v-model="form.cateName" placeholder="请输入节点名称" />
        </el-form-item>
        <el-form-item label="编码" prop="cateCode">
          <el-input v-model="form.cateCode" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="服务地址" prop="ctxUrl">
          <el-input v-model="form.ctxUrl" placeholder="请输入Url前缀" />
        </el-form-item>
        <el-form-item label="appId" prop="appId">
          <el-input v-model="form.appId" placeholder="appId" />
        </el-form-item>
        <el-form-item label="appSecret" prop="appSecret">
          <el-input v-model="form.appSecret" placeholder="appSecret" />
        </el-form-item>
      </el-form>
      <dialog-footer slot="footer" @ok="submitForm" @cancel="cancel" />
    </el-dialog>
  </div>
</template>

<script>
import { listApp, getApp, delApp, addApp, updateApp } from "@slc/api/slc/categories";
import { debounceWrapper } from "@/utils/zmids";

export default {
  components: { },
  name: "ConnCapabilityCategories",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      classifyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      // 状态数据字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderBy: 'UPDATE_TIME',
        sortDirect: 'DESC',
        cateName: undefined,
      },
      pubKey: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        appName: [
          { required: true, message: "节点名称不能为空", trigger: "blur" }
        ],
        appDesc: [
          { required: true, message: "编码不能为空", trigger: "blur" },
        ]
      }
    };
  },
  created () {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询参数列表 */
    getList: debounceWrapper(function () {
      this.loading = true;
      listApp(this.queryParams).then(response => {
        this.classifyList = response.data;
        // this.total = response.data.total;
        this.loading = false;
      }
      ).catch(response => {
        console.log(response);
        this.loading = false;
      });
    }),
    // 参数系统内置字典翻译
    stateFormat (row, column) {
      return this.selectDictLabel(this.stateOptions, row.isEnable);
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        cateParentId: '0',
        cateParentName: '根节点',
        cateName: undefined,
        cateCode: undefined,
        ctxUrl: undefined,
        appId: undefined,
        appSecret: undefined,
        isAdd: true
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset();
      console.log(row);
      if (row) {
        this.form.cateParentId = row.cateId
        this.form.cateParentName = row.cateName
      }
      this.open = true;
      this.title = "添加节点";
    },
    /** 修改按钮操作 */
    handleUpdate: debounceWrapper(function (row) {
      this.reset();
      getApp(row.cateId).then(response => {
        this.form = response.data;
        this.form.isAdd = false;
        this.open = true;
        this.title = "修改应用";
      });
    }),
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.isAdd) {
            updateApp(this.form).then(response => {
              if (response.data === 1) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addApp(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess(response.data);
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      delApp(row.cateId)
        .then(() => {
          this.msgSuccess("删除成功");
          this.getList();
        }).catch(function () {
        });
    },
    /** 删除按钮操作 */
    // handleResetKey (row) {
    //   resetKey(row.appKey)
    //     .then(() => {
    //       this.msgSuccess("重置成功");
    //       this.getList();
    //     }).catch(function () {
    //     });
    // },
    /** 查看公钥按钮操作 */
    handleView (row) {
      this.openDetail = true;
      this.pubKey = row.appPubKey;
    },
  }
};
</script>
